package com.ss.user.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.ss.common.util.MyToolUtil;
import com.ss.user.pojo.Authority;
import com.ss.user.pojo.Role;
import com.ss.user.pojo.RoleAuthority;
import com.ss.user.service.RoleService;
import com.ss.user.service.impl.AuthorityServiceImpl;
/**
 * 角色管理Controller
 */
@Controller("roleController")
@RequestMapping("/user")
public class RoleController {
	@Autowired
	private RoleService roleService;
	@Autowired
	private AuthorityServiceImpl authorityService;
	/**
	 * 进入查询页面
	 * @param request
	 * @param response
	 * @param info
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/initRole.do")
	public ModelAndView toPage(HttpServletRequest request,
			HttpServletResponse response,Role roleInfo) throws Exception {		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		roleInfo.init(request);
		resultMap.put("pageResult", roleInfo.pageResult);
		return new ModelAndView("system/role",resultMap);		
	}
	/**
	 * 分页查询
	 * @param request
	 * @param response
	 * @param info
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/queryRole.do")
	public ModelAndView searchRoleManage(HttpServletRequest request,
			HttpServletResponse response,Role roleInfo) throws Exception {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		roleInfo.init(request);
		roleService.queryRole(roleInfo);
		resultMap.put("pageResult", roleInfo.pageResult);
		return new ModelAndView("system/role",resultMap);		
	}
	/**
	 * 新增角色初始化
	 * @param request
	 * @param response
	 * @param roleInfo
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/addRoleInit.do")
	public ModelAndView addRoleInit(HttpServletRequest request,
			HttpServletResponse response,Role roleInfo) throws Exception {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		return new ModelAndView("system/role_add",resultMap);		
	}
	/**
	 * 新增角色
	 * @param request
	 * @param response
	 * @param roleInfo
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/addRole.do")
	public ModelAndView addRoleManage(HttpServletRequest request,
			HttpServletResponse response,Role roleInfo) throws Exception {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		roleInfo.init(request);
		roleInfo.setStatus("0");
		roleService.addRole(roleInfo);
		resultMap.put("pageResult", "success");
		return new ModelAndView("system/role_add",resultMap);		
	}
	/**
	 * 更新角色
	 * @param request
	 * @param response
	 * @param roleInfo
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/updateRole.do")
	public ModelAndView updateRoleManage(HttpServletRequest request,
			HttpServletResponse response,Role roleInfo) throws Exception {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		roleInfo.init(request);
		roleService.updateRole(roleInfo);
		resultMap.put("pageResult", "success");
		return new ModelAndView("system/role_edit",resultMap);		
	}
	/**
	 * 更新角色初始化
	 * @param request
	 * @param response
	 * @param roleInfo
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/updateRoleInit.do")
	public ModelAndView updateRoleInit(HttpServletRequest request,
			HttpServletResponse response,Role roleInfo) throws Exception {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		roleInfo.init(request);
		String id = request.getParameter("id");
		Role returnInfo = roleService.queryRoleById(id);
		resultMap.put("returnInfo", returnInfo);
		return new ModelAndView("system/role_edit",resultMap);		
	}
	/**
	 * 更新角色功能关联初始化
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/updateRoleAuthorityInit.do")
	public ModelAndView updateRoleAuthorityInit(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		String id = MyToolUtil.convertNull(request.getParameter("id"));
		RoleAuthority roleAuthority = new RoleAuthority();
		roleAuthority.setRoleId(Integer.valueOf(id));
		List<RoleAuthority> returnInfo = roleService.queryRoleAuthority(roleAuthority);
		resultMap.put("returnInfo", returnInfo);
		Role role = roleService.queryRoleById(id);
		resultMap.put("role", role);
		List<Authority> listYes = authorityService.queryAuthorityYes(id);
		resultMap.put("authorityListYes", listYes);
		List<Authority> listNo = authorityService.queryAuthorityNo(id);
		resultMap.put("authorityListNo", listNo);
		return new ModelAndView("system/roleAuthority",resultMap);		
	}
	/**
	 * 更新角色功能关联
	 * @param request
	 * @param response
	 * @param roleInfo
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/updateRoleAuthority.do")
	public ModelAndView updateRoleAuthority(HttpServletRequest request,
			HttpServletResponse response,Role roleInfo) throws Exception {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		String rid = MyToolUtil.convertNull(request.getParameter("rid"));
		String id = MyToolUtil.convertNull(request.getParameter("id"));
		String [] ids = id.substring(0, id.length()-1).split(":");
		RoleAuthority roleAuthority = new RoleAuthority();
		roleAuthority.setRoleId(Integer.valueOf(rid));
		roleService.deleteRoleAuthority(roleAuthority);
		for(int i=0;i<ids.length;i++){
			RoleAuthority ra = new RoleAuthority();
			ra.setRoleId(Integer.valueOf(rid));
			ra.setAuthorityId(Integer.valueOf(ids[i]));
			roleService.addRoleAuthority(ra);
		}
		resultMap.put("pageResult", "success");
		return new ModelAndView("system/roleAuthority",resultMap);		
	}
}
